package com.maimang.platform.gen.service.impl;

import com.maimang.platform.common.config.Global;
import com.maimang.platform.gen.entity.ColumnInfo;
import com.maimang.platform.gen.entity.TableInfo;
import com.maimang.platform.gen.mapper.GenMapper;
import com.maimang.platform.gen.service.GenService;
import com.maimang.platform.gen.util.GenUtils;
import com.maimang.platform.lang.StringUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成 服务层处理
 *
 * @author Rocky
 */
@Service
public class GenServiceImpl implements GenService {
	private static final Logger log = LoggerFactory.getLogger(GenServiceImpl.class);

	@Autowired
	private GenMapper genMapper;

	/**
	 * 查询ry数据库表信息
	 *
	 * @param tableInfo 表信息
	 * @return 数据库表列表
	 */
	@Override
	public List<TableInfo> selectTableList(TableInfo tableInfo) {
		return genMapper.selectTableList(tableInfo);
	}

	/**
	 * 生成代码
	 *
	 * @param tableName 表名称
	 * @return 数据
	 */
	@Override
	public byte[] generatorCode(String tableName) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		// 查询表信息
		TableInfo table = genMapper.selectTableByName(tableName);
		// 查询列信息
		List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName);
		// 生成代码
		log.info(StringUtils.format("正在生成表{}"), tableName + "相关代码");
		GenUtils.generatorCode(table, columns, zip);
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	/**
	 * 批量生成代码
	 *
	 * @param tableNames 表数组
	 * @return 数据
	 */
	@Override
	public byte[] generatorCode(String[] tableNames) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		for (String tableName : tableNames) {
			generatorOneTable(tableName, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	private void generatorOneTable(String tableName, ZipOutputStream zip) {
		// 查询表信息
		TableInfo table = genMapper.selectTableByName(tableName);
		// 查询列信息
		List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName);
		// 生成代码
		log.info(StringUtils.format("正在生成表{}"), tableName + "相关代码");
		GenUtils.generatorCode(table, columns, zip);
	}

	/**
	 * 把参数保存到yaml文件中
	 *
	 * @param map
	 */
	@Override
	public void modifySave(Map<String, Object> map) {
		String fileName = Global.GEN_CONFIG;
		Global.setConfig(fileName, "gen.author", (String) map.get("author"));
		Global.setConfig(fileName, "gen.email", (String) map.get("email"));
		Global.setConfig(fileName, "gen.packageName", (String) map.get("package"));
		Global.setConfig(fileName, "gen.moduleName", (String) map.get("module"));
		Global.setConfig(fileName, "gen.autoRemovePre", (String) map.get("autoRemovePre"));
		Global.setConfig(fileName, "gen.tablePrefix", (String) map.get("tablePrefix"));
	}
}
